/**
 * List widget creator.
 * 
 * Implements a list which can be populated easily.
 * List item:
 * { 
 *   onclick: "script", 
 *   name: "name"
 * }
 * or
 * {
 *   content: "html"
 * }
 */

function ListWidget(options) {
	if(!options) options = {};
	if(!options.id) options.id = Widget.uid();
	
	var that = Widget(options);

	that.getHtml = function() {
		return sprintf('<div class="list" id="%s"></div>', options.id);
	};
	
	that.populate = function(values) {
		var html = [];
		if(values && values.length) {
			for(var i = 0; i < values.length; i++) {
				var c = values[i].clazz || 'list-item';
				if(values[i].content) {
					html.push(sprintf('<div class="%s">%s</div>', c, values[i].content));
				} else {
					html.push(sprintf('<div class="%s" onclick="%sreturn false;">%s</div>', c, values[i].onclick, values[i].name));
				}
			}
		} else if(options.emptyContent) html.push(options.emptyContent);
		this.getContainer().html(html.join(""));
	};
	
	that.size = function() {
		return this.getContainer().children('.list-item').size();
	};
	
	return that;
}
